package com.canbot.u05.activity.vipguide;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.canbot.u05.MainApplication;
import com.canbot.u05.R;
import com.canbot.u05.activity.BaseActivityNoTitle;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.utils.Logger;
import com.canbot.u05.utils.OkHttpUtils;
import com.canbot.u05.utils.PathConst;
import com.canbot.u05.utils.SPUtils;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.widget.LoadingDialogUtils;

import java.util.HashMap;


/**
 * 设置贵宾接待动作
 */
public class ReceptionSettingActivity extends BaseActivityNoTitle implements View.OnClickListener {


        private CheckBox dance;

        private CheckBox salute;

        private CheckBox handshake;

        private String danceName;

        private String isSetting;

        private Dialog dialogLoading;

        private int chooseNum = 0;

        private TextView oneText;

        private TextView twoText;

        private TextView threeText;

        private String[] receptions;

        private boolean isFirstDance =false;

        private String exitDanceName;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_reception_setting);

                String[] voices = {"u05_jdlc_001.wav", "u05_jdlc_002.wav"};
                int index = (int) (Math.random() * voices.length);
                if (index == 0) {
                        MsgSendUtils.sendStringMsg(MsgType.ACTION, "14006");
                }
                else {
                        MsgSendUtils.sendStringMsg(MsgType.ACTION, "14007");
                }
                MsgSendUtils.sendStringMsg(MsgType.PLAY_SOUND, PathConst.VIP_GUIDE_DIR + voices[index]);

                setViews();

                isSetting = getIntent().getStringExtra("isSetting");
        }

        private void setViews() {
                findViewById(R.id.ok_btn).setOnClickListener(this);
                findViewById(R.id.imageView_back).setOnClickListener(this);

                dance = findViewById(R.id.dance);
                salute = findViewById(R.id.salute);
                handshake = findViewById(R.id.handshake);
                dance.setOnClickListener(this);
                salute.setOnClickListener(this);
                handshake.setOnClickListener(this);

                oneText = findViewById(R.id.one_text);
                twoText = findViewById(R.id.two_text);
                threeText = findViewById(R.id.three_text);
        }

        @Override
        protected void onResume() {
                super.onResume();

                if (TextUtils.isEmpty(isSetting)) {
                        if (isFirstDance) {
                                showDanceBackground();
                        }else{
                                showDate();
                        }
                        return;
                }

                if (isFirstDance) {
                        showDanceBackground();
                }
        }

        /**
         * 更新舞蹈背景
         */
        private void showDanceBackground() {
                danceName = SPUtils.getString(ReceptionSettingActivity.this, "danceName", null);
                if (!TextUtils.isEmpty(danceName)) {
                        dance.setTextColor(getResources().getColor(R.color.vip_bg));
                        dance.setBackgroundResource(R.drawable.btn_share_nor);

                        exitDanceName = danceName;
                        if ("跳舞".equals(oneText.getText()) || "跳舞".equals(twoText.getText())
                                || "跳舞".equals(threeText.getText())) {
                        }
                        else {
                                chooseNum = chooseNum + 1;
                                setaddReception(chooseNum, "跳舞");
                        }
                        return;
                }
                if (TextUtils.isEmpty(danceName)) {
                        dance.setTextColor(getResources().getColor(R.color.white));
                        dance.setBackgroundResource(R.drawable.vip_btn_press);

                        if (chooseNum != 0 && !TextUtils.isEmpty(exitDanceName)) {
                                chooseNum = chooseNum - 1;
                                exitDanceName = null;
                                setReception(chooseNum, "跳舞");
                        }
                        return;
                }
                if (!TextUtils.isEmpty(exitDanceName)) {
                        dance.setTextColor(getResources().getColor(R.color.vip_bg));
                        dance.setBackgroundResource(R.drawable.btn_share_nor);
                }
        }

        /**
         * 显示已设置过的动作数据
         */
        private void showDate() {

                String receptionaction = getIntent().getStringExtra("receptionaction");
                Logger.e("123","-------"+receptionaction);
                if(!TextUtils.isEmpty(receptionaction)){
                        receptions = receptionaction.split("&");
                        for (int i = 0; i < receptions.length; i++) {
                                if ("敬礼".equals(receptions[i])) {
                                        chooseNum = i + 1;
                                        salute.setChecked(true);
                                        salute.setTextColor(getResources().getColor(R.color.vip_bg));
                                        setaddReception(chooseNum, "敬礼");
                                }
                                else if ("握手".equals(receptions[i])) {
                                        chooseNum = i + 1;
                                        handshake.setChecked(true);
                                        handshake.setTextColor(getResources().getColor(R.color.vip_bg));
                                        setaddReception(chooseNum, "握手");
                                }
                                else if (!TextUtils.isEmpty(receptions[i]) && !"null".equals(receptions[i])) {
                                        chooseNum = i + 1;
                                        dance.setChecked(true);
                                        dance.setTextColor(getResources().getColor(R.color.vip_bg));
                                        setaddReception(chooseNum, "跳舞");
                                        if(TextUtils.isEmpty(isSetting) && isFirstDance){

                                        }else{
                                                exitDanceName = receptions[i];
                                        }

                                }


                                Logger.e("123780",receptions[i]+"---"+receptions.length);
                        }

                }
        }

        /**
         * 修改接待动作
         */
        public void showHint(String content) {
                dialogLoading = LoadingDialogUtils.createLoadingDialog(ReceptionSettingActivity.this, getResources().getString(R.string.request));
                HashMap<String, Object> paramsMap = new HashMap<String, Object>();

                //音频
                paramsMap.put("vipType", "updateRoute");
                paramsMap.put("identification", getIntent().getStringExtra("identification"));
                paramsMap.put("receptionaction", content);

                OkHttpUtils.upLoadFile("http://" + IpMsgConst.SOCKET_HOST2 + ":7755/" + "routevipGuide", paramsMap, new OkHttpUtils.OkHttpCallBack() {
                        @Override
                        public void onSuccess(String result) {
                                if (!TextUtils.isEmpty(isSetting)) {
                                        startActivity(new Intent(ReceptionSettingActivity.this, SiteSettingActivity.class)
                                                .putExtra("isSetting", isSetting)
                                                .putExtra("identification", getIntent().getStringExtra("identification"))
                                                .putExtra("isChoose", getIntent().getStringExtra("isChoose")));
                                }
                                else {
                                        MainApplication.finishActivity(ReceptionSettingActivity.class.getName());
                                }
                        }

                        @Override
                        public void onFailure() {
                                runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                                cancelDialog();
                                                ToastTools.newInstance(ReceptionSettingActivity.this).showText(getString(R.string.network_error));
                                        }
                                });
                        }
                });

        }

        /**
         * 取消加载的Dialog
         */
        private void cancelDialog() {
                if (dialogLoading != null) {
                        if (dialogLoading.isShowing()) {
                                dialogLoading.cancel();
                        }
                }
        }

        @Override
        public void onClick(View v) {
                switch (v.getId()) {
                        case R.id.imageView_back:
                                finish();
                                break;
                        case R.id.ok_btn:
                                String receptionText = null;

                                if(TextUtils.isEmpty(isSetting) && !isFirstDance){
                                        danceName = exitDanceName;
                                }

                                if ("跳舞".equals(oneText.getText().toString())) {
                                        receptionText = danceName + "&" + twoText.getText().toString() + "&" + threeText.getText().toString();
                                }else if ("跳舞".equals(twoText.getText().toString())) {
                                        receptionText = oneText.getText().toString() + "&" + danceName + "&" + threeText.getText().toString();
                                }else if ("跳舞".equals(threeText.getText().toString())) {
                                        receptionText = oneText.getText().toString() + "&" + twoText.getText().toString() + "&" + danceName;
                                }else{
                                        receptionText = oneText.getText().toString() + "&" + twoText.getText().toString() + "&" + threeText.getText().toString();
                                }

                                Logger.e("12370",receptionText+"---");

                                if(TextUtils.isEmpty(receptionText)){
                                        if (!TextUtils.isEmpty(isSetting)) {
                                                startActivity(new Intent(ReceptionSettingActivity.this, SiteSettingActivity.class)
                                                        .putExtra("isSetting", isSetting)
                                                        .putExtra("identification", getIntent().getStringExtra("identification"))
                                                        .putExtra("isChoose", getIntent().getStringExtra("isChoose")));
                                        }
                                        else {
                                                MainApplication.finishActivity(ReceptionSettingActivity.class.getName());
                                        }
                                }else{
                                        showHint(receptionText);
                                }
                                break;
                        case R.id.dance:
                                isFirstDance = true;
                                startActivity(new Intent(ReceptionSettingActivity.this, ReceptionSettingDanceActivity.class)
                                .putExtra("exitDanceName",exitDanceName));
                                break;
                        case R.id.salute:
                                setTextColor(salute, "敬礼");
                                break;
                        case R.id.handshake:
                                setTextColor(handshake, "握手");
                                break;
                }

        }

        private void setTextColor(CheckBox checkBox, String text) {
                if (checkBox.isChecked()) {
                        chooseNum = chooseNum + 1;
                        checkBox.setTextColor(getResources().getColor(R.color.vip_bg));
                        setaddReception(chooseNum, text);
                }
                else {
                        chooseNum = chooseNum - 1;
                        checkBox.setTextColor(getResources().getColor(R.color.white));
                        setReception(chooseNum, text);
                }
        }

        private void setReception(int num, String text) {

                String onestring = oneText.getText().toString();
                String twostring = twoText.getText().toString();
                String threeString = threeText.getText().toString();

                Logger.e("123", onestring + twostring + threeString + num + text);
                if (num == 1) {
                        findViewById(R.id.greater_than).setVisibility(View.GONE);
                        findViewById(R.id.two).setVisibility(View.GONE);

                        oneText.setText(null);
                        twoText.setText(null);

                        if (!TextUtils.isEmpty(twostring) && !TextUtils.isEmpty(threeString)) {

                                if (text.equals(threeString)) {
                                        oneText.setText(twostring);
                                }
                                if (text.equals(twostring)) {
                                        oneText.setText(threeString);
                                }
                        }
                        if (!TextUtils.isEmpty(threeString) && !TextUtils.isEmpty(onestring)) {

                                if (text.equals(threeString)) {
                                        oneText.setText(onestring);
                                }
                                if (text.equals(onestring)) {
                                        oneText.setText(threeString);
                                }
                        }
                        if (!TextUtils.isEmpty(twostring) && !TextUtils.isEmpty(onestring)) {

                                if (text.equals(twostring)) {
                                        oneText.setText(onestring);
                                }
                                if (text.equals(onestring)) {
                                        oneText.setText(twostring);
                                }
                        }

                }
                else if (num == 2) {
                        findViewById(R.id.greater_than_one).setVisibility(View.GONE);
                        findViewById(R.id.three).setVisibility(View.GONE);

                        oneText.setText(null);
                        twoText.setText(null);
                        threeText.setText(null);

                        if (text.equals(threeString)) {
                                oneText.setText(onestring);
                                twoText.setText(twostring);
                        }
                        if (text.equals(twostring)) {
                                oneText.setText(onestring);
                                twoText.setText(threeString);
                        }
                        if (text.equals(onestring)) {
                                oneText.setText(twostring);
                                twoText.setText(threeString);
                        }
                }
                else if (num == 0) {
                        findViewById(R.id.one).setVisibility(View.GONE);
                        oneText.setText(null);
                }
        }

        private void setaddReception(int num, String text) {
                Logger.e("1237", num+oneText.getText().toString()+twoText.getText().toString()+threeText.getText().toString()+isFirstDance+exitDanceName);
                if (num == 1) {
                        findViewById(R.id.one).setVisibility(View.VISIBLE);
                        oneText.setText(text);
                }
                else if (num == 2) {
                        findViewById(R.id.greater_than).setVisibility(View.VISIBLE);
                        findViewById(R.id.two).setVisibility(View.VISIBLE);
                        twoText.setText(text);
                }
                else if (num == 3) {
                        findViewById(R.id.greater_than_one).setVisibility(View.VISIBLE);
                        findViewById(R.id.three).setVisibility(View.VISIBLE);
                        threeText.setText(text);
                }
        }

        @Override
        protected void onDestroy() {
                super.onDestroy();
                isFirstDance = false;
        }
}
